﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using NetMicro.EventBus.Messages;

namespace NetMicro.EventBus.Persistences
{
    /// <summary>
    /// 仓储
    /// </summary>
    public interface IRetryMessageStorage
    {
        /// <summary>
        /// 保存重试消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<bool> SaveAsync(MessageStorageTransfer message,
          CancellationToken cancellationToken = default);

        /// <summary>
        /// 修改下次重试时间
        /// </summary>
        /// <param name="id"></param>
        /// <param name="retryCount"></param>
        /// <param name="timeSpan"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<bool> NextAsync(long id, int retryCount, TimeSpan timeSpan,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 重试消息添加锁
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<bool> LockAsync(long id,
          CancellationToken cancellationToken = default);

        /// <summary>
        /// 删除重试完成消息
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<bool> DeleteMessageAsync(long id, IDbTransaction transaction = null, CancellationToken cancellationToken = default);

        /// <summary>
        /// 重试消息锁到期之后未释放的锁,释放锁
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<bool> ExpireTimeUnLockAsync(
          CancellationToken cancellationToken = default);

        /// <summary>
        /// 获取需要重试的消息
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        Task<IEnumerable<MessageStorageTransfer>> GetMessagesAsync();

        /// <summary>
        /// 删除到期消息
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<bool> DeleteExpiresAsync(CancellationToken cancellationToken = default);
    }
}
